package com.tianqu.tms.www.rental.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.rental.actionform.RentalContractAddInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;

/**
 * 合同查询/修改 - 信息查看 controller
 * @author mufeiyu
 */
public class RentalContractSearchConfirmController extends TmsController {
	private Logger logger = Logger.getLogger(RentalContractSearchConfirmController.class);
	
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			RentalContractAddInputViewHelper viewhelper = new RentalContractAddInputViewHelper();
			RentalContractAddInputForm rentalForm = (RentalContractAddInputForm) uc.getInputForm();
			
			RentalContractInfo contractInfo = RentalContractInfo.getInstance();
			//合同详细信息查询
			if(uc.getAttribute("confirmForm") != null){
				rentalForm = (RentalContractAddInputForm) uc.getAttribute("confirmForm");
				contractInfo = contractInfo.searchRentalContractDetailInfo(rentalForm.getId());

			}else {
				contractInfo = contractInfo.searchRentalContractDetailInfo(rentalForm.getId());
			}
			//查询结果
			viewhelper = setDetailInfo(contractInfo,viewhelper);
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			uc.setAttribute("confirmForm",rentalForm);
			if(uc.getAttribute("serialsave") != null){
				uc.removeAttribute("serialsave");
			}	
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 信息查看页面表示数据设定
	 * @param dto
	 * @param viewhelper
	 * @throws TmsDaoException
	 */
	private RentalContractAddInputViewHelper setDetailInfo(RentalContractInfo contractInfo,RentalContractAddInputViewHelper viewhelper) throws TmsDaoException{
		RentalLocationInfo locationInfo = RentalLocationInfo.getInstance();
		locationInfo = locationInfo.searchLocationDetailBySerial(contractInfo.getLocationSerial());
		// 合同基本信息
		
		//合同ID
		viewhelper.setId(contractInfo.getId());
		
		//合同编号
		viewhelper.setSerial(contractInfo.getSerial());
		
		//合同状态
		Codes codes = Codes.getInstance();
		viewhelper.setContractStatus(codes.getItemValueByItemIdAndItemCode(Codes.CONTRACT_STATUS, contractInfo.getStatus()));

		viewhelper.setStatus(contractInfo.getStatus());
		if(contractInfo.getStatus() == 1 || contractInfo.getStatus() == 2){
			viewhelper.setButtonFlag("true");
		} else {
			viewhelper.setButtonFlag("false");
		}
		
		//租金
		viewhelper.setRent(StringUtil.getPointBetweenThree(contractInfo.getRent()));
		
		//开始时间
		viewhelper.setStartDate(DateUtil.getDateByTimeStamp(contractInfo.getStartDate()));
		
		//结束时间
		viewhelper.setEndDate(DateUtil.getDateByTimeStamp(contractInfo.getEndDate()));
		
		//租位编号
		viewhelper.setLocationSerial(contractInfo.getLocationSerial());
		
		//租位类型
		Codes code = Codes.getInstance();
		String rentalType = code.getItemValueByItemIdAndItemCode(Codes.RENTAL_TYPE, locationInfo.getRentalType());
		viewhelper.setRentalLocationType(rentalType);
		viewhelper.setRentalType(locationInfo.getRentalType());
		
		//租位面积
		viewhelper.setAcreage(StringUtil.getPointBetweenThree(locationInfo.getAcreage()));
		
		//租位区域
		viewhelper.setRentalArea(locationInfo.getArea());
		viewhelper.setRentalAreaName(code.getItemValueByItemIdAndItemCode(Codes.RENTAL_AREA, locationInfo.getArea()));
				
		//车位数量
		viewhelper.setCarPort(StringUtil.getPointBetweenThree(locationInfo.getCarPort()));
		
		//合同人姓名
		viewhelper.setOwnerName(contractInfo.getOwnerName());
		
		//合同人身份证号
		viewhelper.setOwnerIdCardNo(contractInfo.getOwnerIdCardNo());
		
		//合同人性别
		if(contractInfo.getOwnerSex() == 1){
			viewhelper.setOwnerSex("男");
		}else if(contractInfo.getOwnerSex() == 2){
			viewhelper.setOwnerSex("女");
		}
		
		//合同人生日
		viewhelper.setOwnerBirthday(DateUtil.getDateByTimeStamp(contractInfo.getOwnerBirthday()));
		
		//合同人电话
		viewhelper.setOwnerPhone(contractInfo.getOwnerPhone());
		
		//合同地址
		viewhelper.setOwnerAddress(contractInfo.getOwnerAddress());
		
		//插入操作员
		User user = User.getInstance();
		user = user.getUserInfoById(contractInfo.getInsertUserId());
		viewhelper.setInsertUserName(user.getRealName());
		viewhelper.setInsertUserId(contractInfo.getInsertUserId());
		
		//插入时间
		viewhelper.setInsertTime(DateUtil.getDateByTimeStamp(contractInfo.getInsertTime()));
		
		//更新操作员
		user = user.getUserInfoById(contractInfo.getUpdateUserId());
		viewhelper.setUpdateUserName(user.getRealName());
		viewhelper.setUpdateUserId(contractInfo.getUpdateUserId());
		
		//更新时间
		viewhelper.setUpdateTime(DateUtil.getDateByTimeStamp(contractInfo.getUpdateTime()));
		
		//备注
		viewhelper.setRemark(contractInfo.getRemark());
		
		//资料图片
		viewhelper.setPhoto_1(contractInfo.getPhoto_1());		
		viewhelper.setPhoto_2(contractInfo.getPhoto_2());		
		viewhelper.setPhoto_3(contractInfo.getPhoto_3());		
		viewhelper.setPhoto_4(contractInfo.getPhoto_4());		
		viewhelper.setPhoto_5(contractInfo.getPhoto_5());		
		viewhelper.setPhoto_6(contractInfo.getPhoto_6());		
		viewhelper.setPhoto_7(contractInfo.getPhoto_7());		
		viewhelper.setPhoto_8(contractInfo.getPhoto_8());
		return viewhelper;
	}
}
